package com.wc;

import com.wc.domain.Dept;
import com.wc.domain.Emp;
import com.wc.meiju.Role;
import com.wc.fake.DB;
import com.wc.service.DeptService;
import com.wc.service.EmpService;
import com.wc.service.impl.DeptServiceImpl;
import com.wc.service.impl.EmpServiceImpl;

import java.math.BigDecimal;
import java.util.Scanner;


/**
 * @author wanghao
 */
public class App {
    private final EmpService empService = new EmpServiceImpl();

    private final DeptService deptService = new DeptServiceImpl();

    public static void main(String[] args) {

        App app = new App();
        Scanner scanner = new Scanner(System.in);
        Role role = null;
        int empno = -1;
        do {
            empno = app.login(scanner);
            role = app.empService.getRoleByEmpno(empno);
            System.out.println();
            if (role == null) {
                System.out.println("您输入的账号或密码错误，请重新输入.......");
            }

        } while (role == null);


        int op = -1;
        switch (role) {
            case ADMIN:
                System.out.println("管理员");
                System.out.println("选择操作：1查询指定编号的员工的信息  2修改指定编号的员工的信息  3添加用户 4删除用户 5添加部门  6删除部门 7查寻所有的部门信息 8查寻指定部门的员工的信息 -1退出登录");
                op = scanner.nextInt();
                while (op != -1) {
                    switch (op) {
                        case 1:
                            System.out.println("查询员工信息");
                            System.out.print("请输入需要查询的员工的编号：");
                            Integer deptno = scanner.nextInt();
                            System.out.println(app.deptService.selectEmpByEmpno(deptno));
                            break;
                        case 2:
                            System.out.println("修改信息");
                            System.out.print("请输入需要查询的员工的编号：");
                            Integer deptno2 = scanner.nextInt();
                            System.out.println(app.DeptUpdate(scanner, deptno2) ? "修改成功" : "修改失败");
                            break;
                        case 3:
                            System.out.println("添加用户");
                            break;
                        case 4:
                            System.out.println("删除用户");
                            break;
                        case 5:
                            System.out.println("添加部门");
                            break;
                        case 6:
                            System.out.println("删除部门");
                            break;
                        case 7:
                            System.out.println("查寻所有的部门信息");
                            break;
                        case 8:
                            System.out.println("查寻指定部门的员工的信息");
                            break;

                        default:
                            System.out.println("输入错误，请重新输入！");
                            break;
                    }
                    System.out.println("选择操作：1查询指定编号的员工的信息  2修改指定编号的员工的信息  3添加用户 4删除用户 5添加部门  6删除部门 7查寻所有的部门信息 8查寻指定部门的员工的信息 -1退出登录");
                    op = scanner.nextInt();
                }
                break;
            case VIP:
                System.out.println("VIP");
                break;
            case COMMON:
                System.out.println("普通用户");
                System.out.println("选择操作：1查询本人信息  2修改本人信息 3查询本人所在的部门的信息  -1退出登录 ");
                op = scanner.nextInt();
                while (op != -1) {
                    switch (op) {
                        case 1:
                            System.out.println("查询本人信息");
                            Emp emp = app.empService.getEmpByEmpno(empno);
                            System.out.println(emp);
                            break;
                        case 2:
                            System.out.println("修改本人信息");
                            String updateRes = app.updateEmp(scanner, empno);
                            System.out.println(updateRes);
                            break;
                        case 3:
                            System.out.println("查询本人所在的部门的信息");
                            Dept dept = app.empService.getDeptByEmpno(empno);
                            System.out.println(dept);
                            break;
                        default:
                            System.out.println("输入错误，请重新输入！");
                            break;
                    }
                    System.out.println("选择操作：1查询本人信息  2修改本人信息  -1退出登录 ");
                    op = scanner.nextInt();
                }
                break;
            default:
                System.out.println("角色不正确");

        }


        if (scanner != null) {
            scanner.close();
        }
        System.out.println("成功退出登录");
    }

    private boolean DeptUpdate(Scanner scanner, Integer deptno2) {
        System.out.println("请输入修改后的姓名");
        String name = scanner.next();
        System.out.println("请输入修改后的工作");
        String job = scanner.next();
        System.out.println("请输入修改后的上级编号");
        Integer mgr = scanner.nextInt();
        System.out.println("请输入修改后的薪资");
        double sal0 = scanner.nextDouble();
        BigDecimal sal = new BigDecimal(sal0);
        System.out.println("请输入修改后的奖金");
        double comm0 = scanner.nextDouble();
        BigDecimal comm = new BigDecimal(comm0);
        System.out.println("请输入修改后的部门编号");
        int deptno = scanner.nextInt();
        System.out.println("请输入修改后的登录账号");
        String account = scanner.next();
        System.out.println("请输入修改后的登录密码");
        String password = scanner.next();
        System.out.println("请输入修改后的角色");
        String role = scanner.next();
        Emp emp = Emp.builder()
                .empno(deptno2)
                .ename(name)
                .job(job)
                .mgr(mgr)
                .sal(sal)
                .comm(comm)
                .deptno(deptno)
                .account(account)
                .password(password)
                .role(Role.getRoleByName(role))
                .build();
        boolean b = deptService.updateEmp(emp);
        return b;
    }

    /**
     * 修改个人信息
     *
     * @param scanner
     */
    private String updateEmp(Scanner scanner, Integer empno) {
        System.out.println("请输入修改后的姓名");
        String name = scanner.next();
        System.out.println("请输入修改后的工作");
        String job = scanner.next();
        System.out.println("请输入修改后的上级编号");
        Integer mgr = scanner.nextInt();
        System.out.println("请输入修改后的薪资");
        double sal0 = scanner.nextDouble();
        BigDecimal sal = new BigDecimal(sal0);
        System.out.println("请输入修改后的奖金");
        double comm0 = scanner.nextDouble();
        BigDecimal comm = new BigDecimal(comm0);
        System.out.println("请输入修改后的部门编号");
        int deptno = scanner.nextInt();
        System.out.println("请输入修改后的登录账号");
        String account = scanner.next();
        System.out.println("请输入修改后的登录密码");
        String password = scanner.next();
        System.out.println("请输入修改后的角色");
        String role = scanner.next();
        Emp emp = Emp.builder()
                .empno(empno)
                .ename(name)
                .job(job)
                .mgr(mgr)
                .sal(sal)
                .comm(comm)
                .deptno(deptno)
                .account(account)
                .password(password)
                .role(Role.getRoleByName(role))
                .build();
        String res = empService.updateEmp(emp);
        return res;
    }

    /**
     * 用户登录
     *
     * @param scanner
     * @return
     */
    private int login(Scanner scanner) {
        System.out.println("请输入用户名");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        //用户登录
        int empno = empService.login(username, password);
        return empno;
    }

}